package com.leyou.common.advice;

import com.leyou.common.exception.LyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
//此注解增强的Controller，相当于给所有的可以扫描到的controller添加了try
@ControllerAdvice
public class ControllerExceptionAdvice {

    /**
     * 统一异常处理方法，@ExceptionHandler(RuntimeException.class)
     * 声明这个方法处理RuntimeException这样的异常
     * @param re 捕获到的异常
     * @return 返回给页面的状态码和信息
     */
//    @ExceptionHandler(RuntimeException.class)
//    public ResponseEntity<String> handleLyException(RuntimeException re) {
//        return ResponseEntity.status(400).body(re.getMessage());
//    }

    /**
     * 统一异常处理方法，自定义异常LyException
     * @param le
     * @return
     */
    @ExceptionHandler(LyException.class)
    public ResponseEntity<String> handleLyException(LyException le) {
        return ResponseEntity.status(le.getStatus()).body(le.getMessage());
    }
}
